/*
Copyright SecureKey Technologies Inc. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/

package iam

import (
	"git.cloud.inspur.com/ichain/ichain-sdk-go/pkg/providers/core"
	"git.cloud.inspur.com/ichain/ichain-sdk-go/pkg/providers/iam"
	"github.com/golang/protobuf/proto"

	pb_iam "git.cloud.inspur.com/ichain/ichain-protos-go/iam"
	"github.com/pkg/errors"
)

type User struct {
	id                    string
	iamID                 string
	enrollmentCertificate []byte
	privateKey            core.Key
}

func (u *User) Identifier() *iam.IdentityIdentifier {
	return &iam.IdentityIdentifier{IAMID: u.iamID, ID: u.id}
}

func (u *User) Verify(msg []byte, sig []byte) error {
	return errors.New("not implemented")
}

func (u *User) Serialize() ([]byte, error) {
	serializedIdentity := &pb_iam.SerializedIdentity{
		Iamid:   u.iamID,
		IdBytes: u.enrollmentCertificate,
	}
	identity, err := proto.Marshal(serializedIdentity)
	if err != nil {
		return nil, errors.Wrap(err, "marshal serializedIdentity failed")
	}
	return identity, nil
}

func (u *User) EnrollmentCertificate() []byte {
	return u.enrollmentCertificate
}

func (u *User) PrivateKey() core.Key {
	return u.privateKey
}

func (u *User) PublicVersion() iam.Identity {
	return u
}

func (u *User) Sign(msg []byte) ([]byte, error) {
	return nil, errors.New("Sign() function not implemented")
}
